package com.biannuo.szk.common.base;

import com.biannuo.szk.common.enums.ResponseCodeEnum;
import com.biannuo.szk.common.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * Base控制器封装类
 *
 * @author admin
 */
public class BaseResource implements ServletContextAware {

    @Autowired
    private HttpServletRequest request;


    protected ServletContext servletContext;

    @Override
    public void setServletContext(ServletContext servletContext) {

        this.servletContext = servletContext;

    }

    /**
     * 返回给前端的VO封装,默认处理sucess和error,其它的自定义
     *
     * @param code
     * @param msg
     * @param data
     * @param
     * @return
     */
    public static <T> ResponseVO getResponseVO(String code, String msg, T data) {
        return new ResponseVO<T>(code, msg, data);
    }

    public static <T> ResponseVO buildSuccess(T data) {
        return new ResponseVO<T>(ResponseCodeEnum.SUCCESS, data);
    }

    public static ResponseVO buildSuccess() {
        return new ResponseVO(ResponseCodeEnum.SUCCESS);
    }

    public static ResponseVO buildSuccess(String msg) {
        return new ResponseVO(ResponseCodeEnum.SUCCESS.getCode(), msg);
    }

    public static ResponseVO buildSuccess(String code, String msg) {
        return new ResponseVO(code, msg);
    }

    public static <T> ResponseVO buildSuccess(String code, String msg, T data) {
        return new ResponseVO<T>(code, msg, data);
    }

    public static ResponseVO buildSuccess(ResponseCodeEnum responseCodeEnum) {
        return new ResponseVO(responseCodeEnum);
    }

    public static <T> ResponseVO buildError(T data) {
        return new ResponseVO<T>(ResponseCodeEnum.ERROR, data);
    }

    public static ResponseVO buildError() {
        return new ResponseVO(ResponseCodeEnum.ERROR);
    }

    public static ResponseVO buildError(String msg) {
        return new ResponseVO(ResponseCodeEnum.ERROR.getCode(), msg);
    }

    public static ResponseVO buildError(String code, String msg) {
        return new ResponseVO(code, msg);
    }

    public static <T> ResponseVO buildError(String code, String msg, T data) {
        return new ResponseVO<T>(code, msg, data);
    }

    public static ResponseVO buildError(ResponseCodeEnum responseCodeEnum) {
        return new ResponseVO(responseCodeEnum);
    }

    public static <T> ResponseVO buildBadRequest(T data) {
        return new ResponseVO<T>(ResponseCodeEnum.BAD_REQUEST, data);
    }

    public static ResponseVO buildBadRequest() {
        return new ResponseVO(ResponseCodeEnum.BAD_REQUEST);
    }

    public static ResponseVO buildBadRequest(String msg) {
        return new ResponseVO(ResponseCodeEnum.BAD_REQUEST.getCode(), msg);
    }

    public static ResponseVO buildBadRequest(String code, String msg) {
        return new ResponseVO(code, msg);
    }

    public static <T> ResponseVO buildBadRequest(String code, String msg, T data) {
        return new ResponseVO<T>(code, msg, data);
    }

    public static ResponseVO buildBadRequest(ResponseCodeEnum responseCodeEnum) {
        return new ResponseVO(responseCodeEnum);
    }

    /**
     * 获取request
     */
    public HttpServletRequest getRequest() {
        return this.request;
    }

}
